package com.d4p.util
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.getTimer;

	public class SwfRateInfo extends Sprite
	{
		
		private var _frames:int;
		private var _prevTimer:Number;
		private var _curTimer:Number;
		private var _dummy:Sprite;
		
		private var _rateTf:TextField;
		private var _memTf:TextField;
		
		public function SwfRateInfo()
		{
			setLayOut();
			startRate();
		}
		
		private function startRate():void
		{
			_frames 		= 0;
			_prevTimer 		= 0;
			_curTimer 		= 0;
			
			_dummy = new Sprite;
			_dummy.addEventListener(Event.ENTER_FRAME, dispInfo);
		}
		private function dispInfo(e:Event):void
		{
			_frames 	+=1;
			_curTimer	= getTimer();
			if(_curTimer - _prevTimer >= 1000){
				
				//trace("FPS : " +  String(Math.round(_frames*1000/(_curTimer-_prevTimer))));
				
				_rateTf.text = String(Math.round(_frames*1000/(_curTimer-_prevTimer)))
				
				_prevTimer = _curTimer;
				_frames = 0;
			}
			//trace("MEM in MB\m" + String(Math.round(1000*System.totalMemory/1048576)/1000));
			_memTf.text = String(Math.round(1000*System.totalMemory/1048576)/1000);
		}
		private function setLayOut():void
		{
			_rateTf	 = this.getChildByName('tf_0')	as TextField;
			_memTf	 = this.getChildByName('tf_1')	as TextField;
		}
	}
}

